//
//  searchhistory.m
//  xhtios1.0
//
//  Created by macairwkcao on 15/6/11.
//  Copyright (c) 2015年 hlt. All rights reserved.
//

#import "searchhistory.h"

#define HJYSearchRecordFilePath  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"SearchRecord"]


static NSMutableArray * _searchRecordArray;

@implementation searchhistory


+ (void)initialize
{
    _searchRecordArray = [NSKeyedUnarchiver unarchiveObjectWithFile:HJYSearchRecordFilePath];
    if(_searchRecordArray == nil) {
        _searchRecordArray = [NSMutableArray array];
    }
}

+ (NSArray *)getSearchString
{
    return _searchRecordArray;
}

+ (void)saveSearchString:(NSString *)searchString
{
    if (!searchString.length) return;
    
    // 先将要插入的搜索字符串删除
    [_searchRecordArray removeObject:searchString];
    
    // 最后搜索的字符串排在最前面
    [_searchRecordArray insertObject:searchString atIndex:0];
    
    // 将数组保存至沙盒文件
    [NSKeyedArchiver archiveRootObject:_searchRecordArray toFile:HJYSearchRecordFilePath];
}

+ (void)removeAllSearchRecord
{
    [_searchRecordArray removeAllObjects];
    [NSKeyedArchiver archiveRootObject:_searchRecordArray toFile:HJYSearchRecordFilePath];
}







@end
